package Launcher;

import static org.junit.Assert.assertEquals;
import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import org.junit.Test;
import sort.InsertionSort;
import sort.MergeSort;
import sort.QuickSort;
import sort.RegginSort;

public class Bigtests {
	long[] list;
	InsertionSort iss = new InsertionSort();
	MergeSort ms = new MergeSort();
	QuickSort qs = new QuickSort();
	RegginSort rs = new RegginSort();
	long startTime, endTime;
	long[] testList = new long[100000];
	
	/*
	 * fill the list with random number file
	 */
	@Test
	public void fileWith100Numbers(){
		list = new long[100];
		readFile("100");
		assertEquals(100, list.length);
	}
	@Test
	public void fileWith1000Numbers(){
		list = new long[1000];
		readFile("1000");
		assertEquals(1000, list.length);
	}
	@Test
	public void fileWith10000Numbers(){
		list = new long[10000];
		readFile("10000");
		assertEquals(10000, list.length);
	}
	@Test
	public void fileWith100000Numbers(){
		list = new long[100000];
		readFile("100000");
		assertEquals(100000, list.length);
	}
	/*
	 * best case file's
	 */
	@Test
	public void fileWith100Numbersbc(){
		list = new long[100];
		readFile("BC100");
		assertEquals(100, list.length);
	}
	@Test
	public void fileWith1000Numbersbc(){
		list = new long[1000];
		readFile("BC1000");
		assertEquals(1000, list.length);
	}
	@Test
	public void fileWith10000Numbersbc(){
		list = new long[10000];
		readFile("BC10000");
		assertEquals(10000, list.length);
	}
	@Test
	public void fileWith100000Numbersbc(){
		list = new long[100000];
		readFile("BC100000");
		assertEquals(100000, list.length);
	}
	/*
	 * worst case file's
	 */
	@Test
	public void fileWith100Numberswc(){
		list = new long[100];
		readFile("WC100");
		assertEquals(100, list.length);
	}
	@Test
	public void fileWith1000Numberswc(){
		list = new long[1000];
		readFile("WC1000");
		assertEquals(1000, list.length);
	}
	@Test
	public void fileWith10000Numberswc(){
		list = new long[10000];
		readFile("WC10000");
		assertEquals(10000, list.length);
	}
	@Test
	public void fileWith100000Numberswc(){
		list = new long[100000];
		readFile("WC100000");
		assertEquals(100000, list.length);
	}
	/*
	 * run the algorithms @random numbers file
	 */
	@Test
	public void insertionSort(){
		System.out.println("insertionSort van klein naar groot:");
		fileWith100Numbers();
		startTimer();
		iss.insertionSort( list );
		fileWith1000Numbers();
		endTimer();
		startTimer();
		iss.insertionSort( list );
		endTimer();
		fileWith10000Numbers();
		startTimer();
		iss.insertionSort( list );
		endTimer();
		fileWith100000Numbers();
		startTimer();
		iss.insertionSort( list );
		endTimer();
	}
	@Test
	public void mergeSort(){
		System.out.println("mergeSort van klein naar groot:");
		fileWith100Numbers();
		startTimer();
		ms.mergeSort( list );
		fileWith1000Numbers();
		endTimer();
		startTimer();
		ms.mergeSort( list );
		endTimer();
		fileWith10000Numbers();
		startTimer();
		ms.mergeSort( list );
		endTimer();
		fileWith100000Numbers();
		startTimer();
		ms.mergeSort( list );
		endTimer();
	}
	@Test
	public void quickSort(){
		System.out.println("quicksort van klein naar groot:");
		fileWith100Numbers();
		startTimer();
		qs.sort( list );
		fileWith1000Numbers();
		endTimer();
		startTimer();
		qs.sort( list );
		endTimer();
		fileWith10000Numbers();
		startTimer();
		qs.sort( list );
		endTimer();
		fileWith100000Numbers();
		startTimer();
		qs.sort( list );
		endTimer();
	}
	@Test
	public void regginSort(){
		System.out.println("quicksort van klein naar groot:");
		fileWith100Numbers();
		startTimer();
		rs.sort( list );
		fileWith1000Numbers();
		endTimer();
		startTimer();
		rs.sort( list );
		endTimer();
		fileWith10000Numbers();
		startTimer();
		rs.sort( list );
		endTimer();
		fileWith100000Numbers();
		startTimer();
		rs.sort( list );
		endTimer();
	}
	/*
	 * run the algorithms @best case numbers file
	 */
	@Test
	public void insertionSortBC(){
		System.out.println("BEST CASE insertionSort van klein naar groot:");
		fileWith100Numbersbc();
		startTimer();
		iss.insertionSort( list );
		fileWith1000Numbersbc();
		endTimer();
		startTimer();
		iss.insertionSort( list );
		endTimer();
		fileWith10000Numbersbc();
		startTimer();
		iss.insertionSort( list );
		endTimer();
		fileWith100000Numbersbc();
		startTimer();
		iss.insertionSort( list );
		endTimer();
	}
	@Test
	public void mergeSortBC(){
		System.out.println("BEST CASE mergeSort van klein naar groot:");
		fileWith100Numbersbc();
		startTimer();
		ms.mergeSort( list );
		fileWith1000Numbersbc();
		endTimer();
		startTimer();
		ms.mergeSort( list );
		endTimer();
		fileWith10000Numbersbc();
		startTimer();
		ms.mergeSort( list );
		endTimer();
		fileWith100000Numbersbc();
		startTimer();
		ms.mergeSort( list );
		endTimer();
	}
	@Test
	public void quickSortBC(){
		System.out.println("BEST CASE quicksort van klein naar groot:");
		fileWith100Numbersbc();
		startTimer();
		qs.sort( list );
		fileWith1000Numbersbc();
		endTimer();
		startTimer();
		qs.sort( list );
		endTimer();
		fileWith10000Numbersbc();
		startTimer();
		qs.sort( list );
		endTimer();
		fileWith100000Numbersbc();
		startTimer();
		qs.sort( list );
		endTimer();
	}
	@Test
	public void regginsSortBC(){
		System.out.println("BEST CASE quicksort van klein naar groot:");
		fileWith100Numbersbc();
		startTimer();
		rs.sort( list );
		fileWith1000Numbersbc();
		endTimer();
		startTimer();
		rs.sort( list );
		endTimer();
		fileWith10000Numbersbc();
		startTimer();
		rs.sort( list );
		endTimer();
		fileWith100000Numbersbc();
		startTimer();
		rs.sort( list );
		endTimer();
	}
	/*
	 * run the algorithms @worst case numbers file
	 */
	@Test
	public void insertionSortWC(){
		System.out.println("WORST CASE insertionSort van klein naar groot:");
		fileWith100Numberswc();
		startTimer();
		iss.insertionSort( list );
		fileWith1000Numberswc();
		endTimer();
		startTimer();
		iss.insertionSort( list );
		endTimer();
		fileWith10000Numberswc();
		startTimer();
		iss.insertionSort( list );
		endTimer();
		fileWith100000Numberswc();
		startTimer();
		iss.insertionSort( list );
		endTimer();
	}
	@Test
	public void mergeSortWC(){
		System.out.println("WORST CASE mergeSort van klein naar groot:");
		fileWith100Numberswc();
		startTimer();
		ms.mergeSort( list );
		fileWith1000Numberswc();
		endTimer();
		startTimer();
		ms.mergeSort( list );
		endTimer();
		fileWith10000Numberswc();
		startTimer();
		ms.mergeSort( list );
		endTimer();
		fileWith100000Numberswc();
		startTimer();
		ms.mergeSort( list );
		endTimer();
	}
	@Test
	public void quickSortWC(){
		System.out.println("WORST CASE quicksort van klein naar groot:");
		fileWith100Numberswc();
		startTimer();
		qs.sort( list );
		fileWith1000Numberswc();
		endTimer();
		startTimer();
		qs.sort( list );
		endTimer();
		fileWith10000Numberswc();
		startTimer();
		qs.sort( list );
		endTimer();
		fileWith100000Numberswc();
		startTimer();
		qs.sort( list );
		endTimer();
	}
	@Test
	public void regginSortWC(){
		System.out.println("WORST CASE quicksort van klein naar groot:");
		fileWith100Numberswc();
		startTimer();
		rs.sort( list );
		fileWith1000Numberswc();
		endTimer();
		startTimer();
		rs.sort( list );
		endTimer();
		fileWith10000Numberswc();
		startTimer();
		rs.sort( list );
		endTimer();
		fileWith100000Numberswc();
		startTimer();
		rs.sort( list );
		endTimer();
	}
	/*
	 * rest of the bullshit
	 */
	public void readFile(String fileNumber) {
		int counter = 0;
		try{
			FileInputStream fstream = new FileInputStream( fileNumber + ".txt" );
			DataInputStream in = new DataInputStream( fstream );
			BufferedReader br = new BufferedReader( new InputStreamReader( in ) );
			String strLine;
			while( ( strLine = br.readLine() ) != null ){
				list[counter] = Long.parseLong( strLine );
				counter++;
			}
			in.close();
		}catch( Exception e ){
			System.err.println( "Error: " + e.getMessage() );
		}
	}
	private void endTimer() {
		endTime = System.currentTimeMillis();
		long timeNeeded = endTime - startTime;
		System.out.println("time needed in ms: " + timeNeeded);
	}
	private void startTimer() {
		startTime = System.currentTimeMillis();
	}
}
